package com.hliushi.nio.c4;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Random;

/**
 * @author llhuang10
 * @date 2021/10/17 17:38
 */
public class Client {

    public static void main(String[] args) throws IOException {
        test01();
        // test02();
    }

    private static void test01() throws IOException {
        SocketChannel sc = SocketChannel.open();
        sc.connect(new InetSocketAddress("localhost", 10010));
        System.out.println("waiting...");
    }

    private static void test02() throws IOException {
        SocketChannel sc = SocketChannel.open();
        sc.connect(new InetSocketAddress("localhost", 10010));

        Random random = new Random();
        // for (int i = 0; i < random.nextInt(3); i++) {
        //     sc.write(Charset.defaultCharset().encode("hello0" + i));
        // }
        // sc.write(Charset.defaultCharset().encode("hello\nworld\n"));
        sc.write(Charset.defaultCharset().encode("0123456789abcdefg\n"));
        sc.write(Charset.defaultCharset().encode("0123456789abcdefg33333\n"));
        System.in.read();       // 阻塞方法
        System.out.println("waiting...");
    }


}
